package com.javaee6.liimp.controller;

import com.javaee6.liimp.util.SocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class WebSocketController {

    @Autowired
    private SocketServer socketServer;

    //客户端界面
    @RequestMapping(value = "/message")
    public String message(){
        return "message";
    }

    //服务端界面
    @RequestMapping(value = "/admin")
    public  String admin(Model model){
        int num = socketServer.getOnlineNum();
        List<String> list = socketServer.getOnlineUsers();

        model.addAttribute("num",num);
        model.addAttribute("users",list);
        return "admin";
    }

    //个人信息推送
    @RequestMapping("sendmsg")
    @ResponseBody
    public String sendmsg(String msg,String userName){
        //第一个参数:msg 发送的信息内容
        //第二个参数为用户长连接传的用户人数
        String[] persons = userName.split(",");
        SocketServer.sendMany(msg,persons);
        return "success";
    }

    //推送给所有在线用户
    @RequestMapping("sendAll")
    @ResponseBody
    public String dendAll(String msg){

        SocketServer.sendAll(msg);
        return "success!";
    }
}
